import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  // 公共state对象，存储所有组件的状态
    state: {
        sharedData: {name:'',id:''},//接受者信息
        user:{name:'',id:''}//新增：登录用户信息
    },
    //唯一取值的方法，计算属性
    getters: {
        getSharedData: (state) => state.sharedData,
        getCurrentUser:(state)=>state.user
    },
    // 唯一可以修改state值的方法，同步阻塞
    mutations: {
        updateSharedData(state, newData) {
            state.sharedData =newData
        },
        updateCurrentUser(state,user){
            state.user={id:user.id,name:user.name}
        }

    },
    // 异步调用mutations方法
    actions: {
        updateData({ commit }, newData) {
            commit('updateSharedData', newData);
        }
    },
    modules: {}
});    